package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaCase;
import com.lilosoft.business.cga.models.EgsProjectsummary;
import com.lilosoft.business.precity.dao.UpProResultMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProResult;
import com.lilosoft.business.precity.models.UpProResultExample;
import com.lilosoft.core.utils.commons.StringUtils;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 办件结果信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 10:58
 */
@Service
public class UpProResultService {
    @Autowired
    private UpProResultMapper upProResultMapper;

    public UpProResult UpProResultSynchronous(UpProAccept upProAccept, EgsProjectsummary egsProjectsummary, CgaCase cgaCase, Integer materialCount) {
        UpProResult model = new UpProResult();
        BeanUtils.copyProperties(upProAccept, model);
        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());

        if (StringUtils.isEmpty(egsProjectsummary.getResultType())) {
            model.setResultStyle(205);
        } else {
            switch (egsProjectsummary.getResultType()) {
                case "01":
                    model.setResultStyle(202);
                    break;
                case "02":
                    model.setResultStyle(204);
                    break;
                case "04":
                    model.setResultStyle(201);
                    break;
                default:
                    model.setResultStyle(205);
                    break;

            }
        }
        model.setResultcetrname(egsProjectsummary.getResultName());

        if (StringUtils.isEmpty(cgaCase.getCaseState())) {
            model.setResulttype("3");
        } else {
            switch (cgaCase.getCaseState()) {
                case "09":
                    model.setResulttype("1");
                    break;
                case "12":
                case "13":
                    model.setResulttype("0");
                    break;
                default:
                    model.setResulttype("3");
                    break;
            }
        }

        model.setResultCopiesnum(materialCount);
        model.setResultdate(cgaCase.getEndTime());
        model.setIsdeliveryresults("0");
        List<UpProResult> upProResults = selectByProjectNo(model.getProjectno());
        if (null == upProResults  || upProResults.size()<=0) {
            upProResultMapper.insertSelective(model);
        }
        return model;
    }

    public List<UpProResult> selectByProjectNo(String projectNo) {
        UpProResultExample example = new UpProResultExample();
        UpProResultExample.Criteria criteria = example.createCriteria();
        criteria.andProjectnoEqualTo(projectNo);
        criteria.andDelStateEqualTo(0);
        return upProResultMapper.selectByExample(example);
    }

    /**
     * 根据cdBatch 统计数据个数（办件结果信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch) {
        UpProResultExample example = new UpProResultExample();
        UpProResultExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProResultMapper.countByExample(example);
    }
}
